package com.espirit.eap.manager.workflow;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.EntityMapping;
import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.espirit.eap.manager.I18n;
import com.googlecode.cswish.annotation.Protected;
import com.googlecode.cswish.annotation.QLFormula;

@ModelDisplayProperty(value="text")
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size=10)

public class WFStatus extends EntityMapping implements ICheckModelDuplicateInfo {
	
	private static final long serialVersionUID = -2692533651330185177L;
	
	public static final String ACCEPT = "Accept";
	public static final String REJECT = "Reject";
	
	private String code;
	private String name;
	
	private Integer itemNbr;
	
	private I18n i18n;
	
	public WFStatus(){
		
	}
	public WFStatus(int itemNbr, String code,String enName,String cnName) {
		setItemNbr(itemNbr);
		setCode(code);
		safeI18n().setEnName(enName);
		safeI18n().setCnName(cnName);
	}
	
	private Integer sid;
	public Integer getSid() {
		return sid;
	}

	public void setSid(Integer sid) {
		this.sid = sid;
	}
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getItemNbr() {
		return itemNbr;
	}
	public void setItemNbr(Integer itemNbr) {
		this.itemNbr = itemNbr;
	}
	
	@ManyToOne(targetEntity = I18n.class,fetch=FetchType.LAZY,cascade = CascadeType.MERGE)
	public I18n getI18n() {
		return i18n;
	}
	public void setI18n(I18n i18n) {
		this.i18n = i18n;
	}
	
	private final static String[] gatherProperties = new String[]{"code"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	
	@Transient
	public boolean isAccept(){
		return ACCEPT.equals(this.getCode());
	}
	
	@Transient
	public boolean isReject(){
		return REJECT.equals(this.getCode());
	}
	
	public I18n safeI18n(){
		if (i18n == null) {
			i18n = new I18n();
			i18n.setKey(getI18nKey());
		}
		return i18n;
	}
	
	@Transient
	private String getI18nKey(){
		String key =  this.getClass().getSimpleName() + "." + getCode();
		return key;
	}
	
	private String text;
	@Transient
	@Protected
	@QLFormula("i18n.cnName")
	public String getText(){
		if (text == null){
			if (this.getI18n() != null) {
				text = this.getI18n().getName();
			} else {
				text = this.getName();
			}
		} 
		return text;
	}
	
}
